package com.maxatime.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maxatime.pojo.EmployeeRegister;
import com.maxatime.pojo.StudentMessage;
import com.maxatime.pojo.StudentRegister;
import com.maxatime.service.RegisterService;
import com.maxatime.utils.MailService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MailSessionDefinitions;
import javax.mail.Session;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


@Controller
public class RegisterController {

    @Autowired
    private RegisterService registerService;
    @Autowired
    MailService mailService;


    @RequestMapping("/wsx")
    public String wsx(){
        return "zhuce";
    }


    Logger log = LogManager.getLogger();
    @RequestMapping("/register")
    public String register(String employeeName,String email,String password,Integer inter){


        if(inter==1){
            Map<String, Object> map = registerService.saveRegister(employeeName, email, password);
            return "register";
        }
         else if(inter==2){

            Map<String, Object> m = registerService.sRegister(email, password);
             return "register";
        }
         else {
             return "zhuce";
        }

    }





    @PostMapping("/login")
    public String login (String email, String password, HttpSession session, Model model,Integer interest,String checkCode) {

        Object code = session.getAttribute("code");
//       if (code.toString().equals(checkCode)) {
        EmployeeRegister em = registerService.login(email, password);
        StudentRegister emm = registerService.loginn(email, password);
        StudentMessage rrr = registerService.selectKey(emm.getStudentId());
        log.info("ffffffffffffff" + rrr);


        if (interest == 1 & em != null) {

                session.setAttribute("em", em);
                return "/html/list";

        } else if (interest == 2) {

            session.setAttribute("stume", rrr);
            return "/html/studentlist";
        }
             else {
                model.addAttribute("error", "账号或密码错误");
                return "register";
            }

//    }
//    else {
//            model.addAttribute("error", "邮箱验证码错误");
//            return "register";
//          }


    }
    @RequestMapping("/removeUser")
    public  String removeUser(HttpSession session){
        session.removeAttribute("em");
        return "register";
    }
    @RequestMapping("/ddd")
    public String ddd(){
        return "/html/index";
    }


    @RequestMapping("/fff")
    @ResponseBody
    public  Map<String,Object> fff(@RequestBody String str){

            log.info("str==="+str);
           Map<String,Object> m=new HashMap<>();
           m.put("code","0000");
           m.put("msg","交易成功");

        JSONObject jsonObject = JSONObject.parseObject(str);
        String username = jsonObject.getString("username");
        String email = jsonObject.getString("email");
        log.info("username===="+username);
        log.info("email===="+email);



        JSONArray jsonArr = jsonObject.getJSONArray("dept");
        for (int i=0;i<jsonArr.size();i++){

            JSONObject p = jsonArr.getJSONObject(i);
            if ("女".equals(p.getString("sex"))) {


                log.info("name:::::" + p.getString("name"));
                log.info("sex:::" + p.getString("sex"));
                log.info("email:::::" + p.getString("email"));
            }
        }

        JSONObject response = jsonObject.getJSONObject("response");
        JSONObject data = response.getJSONObject("data");
        JSONArray jn = data.getJSONArray("cnameRecords");

        for (int i=0;i<jn.size();i++){

            JSONObject p = jn.getJSONObject(i);



                log.info("aliasHost:::::" + p.getString("aliasHost"));
                log.info("canonicalHost:::" + p.getString("canonicalHost"));

        }



        return m;
    }
    @RequestMapping("/zhuce")
    public String asd(){
        return "zhuce";
    }


      @RequestMapping("/getCheckCode")
      @ResponseBody
       public String getCheckCode(String email, HttpSession session){

        String checkCode=String.valueOf(new Random().nextInt(899999)+100000);
        String message="您注册的验证码为: "+checkCode;
        session.setAttribute("code",checkCode);
        try {
            mailService.sendSimpleMail(email,"注册验证码",message);


        }catch (Exception e){

            return "";
        }

        return checkCode;
      }





}
